package com.server.http.handler.impl;

import com.server.conditionenum.HttpReturnStatus;
import com.server.db.DataBaseManager;
import com.server.http.handler.HttpHandler;
import com.server.http.request.HttpRequestMessage;
import com.server.http.response.HttpResponseMessage;
import com.server.mine.net.HttpProtocol;
import com.server.tools.AppData;

/**
* @author	李小布
* @email	397554852@qq.com
* @version	创建时间：2018年9月28日 上午10:23:46
*/
public class AddAppRanklist extends HttpHandler{
	@Override
	public void handle(HttpRequestMessage request, HttpResponseMessage response) {
		String failMsg = null;
		long appid = 0;
		int resettime = 0;
		String tablename = request.getParameter("tablename");
		if(tablename == null || tablename == "" ) {
			failMsg = "参数错误或参数值为空！";
		}else if(!AppData.JudgeRankTableContains(tablename)){
			try {
				resettime = Integer.parseInt(request.getParameter("resettime"));
				if(resettime >= 0 && resettime <5) {
					appid = Long.parseLong(request.getParameter("appid"));
					if(AppData.appMap.containsKey(appid)) {
						failMsg = DataBaseManager.GetInstance().AddRankList(appid, tablename, resettime);
					}else {
						failMsg = "该应用不存在！";
					}
				}else {
					failMsg = "重置时间值范围是0~4,0-不重置 1-每小时重置一次 2-每天重置一次 3-每周重置一次 4-每月重置一次！";
				}
			} catch (Exception e) {
				e.printStackTrace();
				failMsg = "参数错误或参数值为空！";
			}
		}else {
			failMsg = "该表已存在!";
		}
		if(failMsg == null) {
			SetReturnData(response,"创建成功",HttpProtocol.ADDRANKLIST,HttpReturnStatus.Successed);
		}else {
			SetReturnData(response,failMsg,HttpProtocol.ADDRANKLIST,HttpReturnStatus.Failed);
		}
	}
}